package com.example.common.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * Redis工具类，提供多业务Redis操作功能
 * 
 * @author AI Assistant
 * @since 2025-01-08
 */
@Component
public class RedisUtil {

    @Autowired
    @Qualifier("userCacheRedisTemplate")
    private RedisTemplate<String, Object> userCacheRedisTemplate;

    @Autowired
    @Qualifier("sessionRedisTemplate")
    private RedisTemplate<String, Object> sessionRedisTemplate;

    @Autowired
    @Qualifier("messageQueueRedisTemplate")
    private RedisTemplate<String, Object> messageQueueRedisTemplate;

    /**
     * 获取用户缓存Redis模板
     */
    public RedisTemplate<String, Object> getUserCacheTemplate() {
        return userCacheRedisTemplate;
    }

    /**
     * 获取会话Redis模板
     */
    public RedisTemplate<String, Object> getSessionTemplate() {
        return sessionRedisTemplate;
    }

    /**
     * 获取消息队列Redis模板
     */
    public RedisTemplate<String, Object> getMessageQueueTemplate() {
        return messageQueueRedisTemplate;
    }

    // ========== 用户缓存操作 ==========

    /**
     * 设置用户缓存
     */
    public void setUserCache(String key, Object value) {
        userCacheRedisTemplate.opsForValue().set(key, value);
    }

    /**
     * 设置用户缓存，带过期时间
     */
    public void setUserCache(String key, Object value, long timeout, TimeUnit unit) {
        userCacheRedisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    /**
     * 获取用户缓存
     */
    public Object getUserCache(String key) {
        return userCacheRedisTemplate.opsForValue().get(key);
    }

    /**
     * 获取用户缓存，指定类型
     */
    @SuppressWarnings("unchecked")
    public <T> T getUserCache(String key, Class<T> type) {
        Object value = userCacheRedisTemplate.opsForValue().get(key);
        return value != null ? (T) value : null;
    }

    /**
     * 删除用户缓存
     */
    public Boolean deleteUserCache(String key) {
        return userCacheRedisTemplate.delete(key);
    }

    // ========== 会话操作 ==========

    /**
     * 设置会话信息
     */
    public void setSession(String key, Object value) {
        sessionRedisTemplate.opsForValue().set(key, value);
    }

    /**
     * 设置会话信息，带过期时间
     */
    public void setSession(String key, Object value, long timeout, TimeUnit unit) {
        sessionRedisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    /**
     * 获取会话信息
     */
    public Object getSession(String key) {
        return sessionRedisTemplate.opsForValue().get(key);
    }

    /**
     * 删除会话信息
     */
    public Boolean deleteSession(String key) {
        return sessionRedisTemplate.delete(key);
    }

    // ========== 消息队列操作 ==========

    /**
     * 发送消息到队列
     */
    public void sendMessage(String queue, Object message) {
        messageQueueRedisTemplate.opsForList().leftPush(queue, message);
    }

    /**
     * 从队列接收消息
     */
    public Object receiveMessage(String queue) {
        return messageQueueRedisTemplate.opsForList().rightPop(queue);
    }

    /**
     * 从队列接收消息，带超时
     */
    public Object receiveMessage(String queue, long timeout, TimeUnit unit) {
        return messageQueueRedisTemplate.opsForList().rightPop(queue, timeout, unit);
    }

    /**
     * 获取队列长度
     */
    public Long getQueueLength(String queue) {
        return messageQueueRedisTemplate.opsForList().size(queue);
    }
}
